# -*- coding: utf-8 -*-
#
# Copyright (c) 2007 Benoit Chesneau <benoitc@metavers.net>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User
#from django.utils.encoding import smart_unicode, force_unicode

import re
from datetime import datetime

#------------------------------------------------------------------------------ 
class Comment(models.Model):
    comment = models.TextField()
    user_name = models.CharField(maxlength="100", blank=True)
    #user_name = models.ForeignKey(User)
    content_type = models.ForeignKey(ContentType)
    object_id = models.CharField(maxlength="200")
    pub_date = models.DateTimeField(blank=True, null=True)
    is_removed = models.BooleanField(default=False)
    
    class Meta:
        verbose_name = 'Комментарий'
        verbose_name_plural = 'Комментарии'
        ordering = ('pub_date',)
        
    class Admin:
        list_display = ('user_name', 'pub_date', 'comment', 'is_removed')
        fields=(
        (None, {'fields': ('content_type', 'object_id', 'is_removed')}),
        ('Content', {'fields': ('user_name','comment')}),
        )
        search_fields = ('comment','user_name')
        date_hierarchy = 'pub_date'
        
    def save(self):
        if not self.id:
            self.pub_date = datetime.now()          
        super(Comment , self).save()

    def __str__(self):
        return self.comment

    def __unicode__(self):
        return self.comment
    
    def get_absolute_url(self):
        obj = self.content_type.get_object_for_this_type(pk = self.object_id)
        return "%s#comments" % obj.get_absolute_url()

#------------------------------------------------------------------------------ 
